package com.youi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.RefundorderDao;
import com.youi.entity.Orderaftersale;
import com.youi.entity.Ordergoods;
import com.youi.entity.Refundorder;
import com.youi.entity.Userorder;
import com.youi.entity.Yibaorefund;
import com.youi.entity.vo.ExportRefundOrderVO;
import com.youi.service.OrderaftersaleService;
import com.youi.service.OrdergoodsService;
import com.youi.service.RefundorderService;
import com.youi.service.UserorderService;
import com.youi.service.YibaorefundService;
import com.youi.third.yibaopay.YibaopayUtil;
import com.youi.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RefundorderServiceImpl extends BaseServiceImpl<Refundorder> implements RefundorderService {

    private RefundorderDao refundorderDao;

    @Autowired
    private UserorderService userorderService;
    @Autowired
    private OrdergoodsService ordergoodsService;
    @Autowired
    private OrderaftersaleService orderaftersaleService;
    @Autowired
    private YibaorefundService yibaorefundService;

    @Autowired
    public void setfaultRecordDao(RefundorderDao refundorderDao) {
        this.refundorderDao = refundorderDao;
        super.setBaseDAO(refundorderDao);
    }

    @Override
    public List<String> getRefundOrderNo(Long merchantId) {
        // TODO Auto-generated method stub
        List<String> orderNoList = null;
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put(Common.DELSTATUS, DelStatus.NODEL);
        prams.put(Common.STATUS, Arrays.asList(0, 1, 2, 3, 4, 5));
        if (merchantId != null) {
            prams.put("merchantId", merchantId);
        }
        List<Userorder> userorderList = userorderService.getByParams(prams);
        if (userorderList != null && userorderList.size() > 0) {
            orderNoList = new ArrayList<String>();
            for (Userorder userorder : userorderList) {
                orderNoList.add(userorder.getOrderNo());
            }
        }
        return orderNoList;
    }

    @Override
    public void getRefundorderMsg(List<Refundorder> refundorderList) throws IOException {
        // TODO Auto-generated method stub
        if (refundorderList != null && refundorderList.size() > 0) {
            for (Refundorder refundorder : refundorderList) {
                Map<String, Object> prams = new HashMap<String, Object>();
                prams.put(Common.DELSTATUS, DelStatus.NODEL);
                prams.put("orderNo", refundorder.getOrderNo());
                Userorder userorder = userorderService.getByParam(prams);
                if (userorder != null) {
                    prams = new HashMap<String, Object>();
                    prams.put(Common.DELSTATUS, DelStatus.NODEL);
                    prams.put("orderId", userorder.getId());
                    Ordergoods ordergoods = ordergoodsService.getByParam(prams);
                    if (ordergoods != null) {
                        refundorder.setGoodsName(ordergoods.getName());
                        refundorder.setGoodsCode(ordergoods.getGoodsNo());
                        refundorder.setGoodsCount(ordergoods.getCount());
                    }
                    prams = new HashMap<String, Object>();
                    prams.put(Common.DELSTATUS, DelStatus.NODEL);
                    prams.put("orderId", userorder.getId());
                    prams.put("userId", userorder.getUserId());
                    Orderaftersale orderaftersale = orderaftersaleService.getByParam(prams);
                    if (orderaftersale != null) {
                        refundorder.setOrderaftersale(orderaftersale);
                    }
                }
                prams = new HashMap<String, Object>();
                prams.put(Common.DELSTATUS, DelStatus.NODEL);
                prams.put("orderNo", refundorder.getOrderNo());
                Yibaorefund yibaorefund = yibaorefundService.getByParam(prams);
                if (yibaorefund != null) {
                    JSONObject data = YibaopayUtil.yibaoGetRefund(yibaorefund.getOrderId(), yibaorefund.getUniqueRefundNo(), yibaorefund.getRefundOrderNo());
                    if (data != null && data.getString("code").equals("OPR00000")) {
                        if (data.getString("status").equals("SUCCESS")) {
                            if (userorder.getRefund() == 1) {
                                userorder.setRefund(2);
                                userorderService.saveOrUpdate(userorder);
                                yibaorefund.setStatus(data.getString("status"));
                                yibaorefundService.saveOrUpdate(yibaorefund);
                                refundorder.setStatus(1);
                                refundorderDao.saveOrUpdate(refundorder);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void getRefundorderMsg(Refundorder refundorder) throws IOException {
        // TODO Auto-generated method stub
        if (refundorder != null) {
            Map<String, Object> prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("orderNo", refundorder.getOrderNo());
            Userorder userorder = userorderService.getByParam(prams);
            if (userorder != null) {
                prams = new HashMap<String, Object>();
                prams.put(Common.DELSTATUS, DelStatus.NODEL);
                prams.put("orderId", userorder.getId());
                Ordergoods ordergoods = ordergoodsService.getByParam(prams);
                if (ordergoods != null) {
                    refundorder.setGoodsName(ordergoods.getName());
                    refundorder.setGoodsCode(ordergoods.getGoodsNo());
                    refundorder.setGoodsCount(ordergoods.getCount());
                }
                prams = new HashMap<String, Object>();
                prams.put(Common.DELSTATUS, DelStatus.NODEL);
                prams.put("orderId", userorder.getId());
                prams.put("userId", userorder.getUserId());
                Orderaftersale orderaftersale = orderaftersaleService.getByParam(prams);
                if (orderaftersale != null) {
                    refundorder.setOrderaftersale(orderaftersale);
                }
            }

            prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("orderNo", refundorder.getOrderNo());
            Yibaorefund yibaorefund = yibaorefundService.getByParam(prams);
            if (yibaorefund != null) {
                JSONObject data = YibaopayUtil.yibaoGetRefund(yibaorefund.getOrderId(), yibaorefund.getUniqueRefundNo(), yibaorefund.getRefundOrderNo());
                if (data != null && data.getString("code").equals("OPR00000")) {
                    if (data.getString("status").equals("SUCCESS")) {
                        if (refundorder.getStatus() == 0) {
                            userorder.setRefund(2);
                            userorderService.saveOrUpdate(userorder);
                            yibaorefund.setStatus(data.getString("status"));
                            yibaorefundService.saveOrUpdate(yibaorefund);
                            refundorder.setStatus(1);
                            refundorderDao.saveOrUpdate(refundorder);
                        }
                    }
                }
            }
        }
    }


    @Override
    public List<ExportRefundOrderVO> listExportOrderDatas(Map<String, Object> param) {
        List<ExportRefundOrderVO> list = refundorderDao.listExportOrderDatas(param);
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        list.stream().forEach(refundOrder -> {
            refundOrder.setRefundTypeTxt(refundOrder.getRefundType() != null && refundOrder.getRefundType() == 0 ?
                    "易宝退款" : "未知");
            refundOrder.setRefundMethodsTxt(refundOrder.getRefundMethods() != null && refundOrder.getRefundMethods() == 0 ?
                    "整单退" : "未知");
            refundOrder.setStatusTxt(ObjectUtil.getRefundOrderState(refundOrder.getStatus()));
            refundOrder.setApplyStatusTxt(ObjectUtil.getRefundOrderApplyState(refundOrder.getApplyStatus()));
            refundOrder.setTakeStatusTxt(ObjectUtil.getRefundOrderTakeState(refundOrder.getTakeStatus()));

        });
        return list;
    }
}